import {Component, OnInit} from '@angular/core';
import {ExtendHttpService} from "../../../common/extend-http.service";
import {TipService} from "../../services/tip.service";
import {ActivatedRoute, Router} from "@angular/router";
@Component({
  selector: 'app-order-recall',
  templateUrl: './order-recall.component.html',
  styleUrls: ['./order-recall.component.css']
})
export class OrderRecallComponent implements OnInit {


  // 订单列表
  orderList = [];

  // 提示信息类型
  tipType = 'error';

  // 餐桌id
  tid = '';

  // 总数量
  countNumber = 0;

  // 总价格
  countPrice = 0;

  // 退菜总数量
  recallCount = 0;

  // 退菜总价格
  recallPrice = 0;

  // 订单id
  orderId = '';

  // 订单流水号
  orderNum = '';

  submitType = true;

  isLoading = false;


  constructor(private extendHttp: ExtendHttpService,
              private tipService: TipService,
              private activatedRoute: ActivatedRoute,
              private router: Router) {

    this.activatedRoute.queryParams.subscribe(param => {
      this.orderId = param['orderId'];
      this.tid = param['tid'];
    });
  }

  // 减菜
  reduce(item) {
    if (item.opNum) {
      --item.opNum;
      this.countRecall();
    }
  }

  // 加菜
  add(item) {
    if (item.opNum < item.num) {
      ++item.opNum;
      this.countRecall();
    }
  }

  countRecall() {
    this.recallCount = 0;
    this.recallPrice = 0;
    this.orderList.forEach((v) => {
      let n = 0;
      if (v.opNum == 0) {
        n = v.num;
      } else {
        n = v.num - v.opNum;
      }
      this.recallPrice += n * v.price
      this.recallCount += n;
    });
  }

  count() {
    this.countPrice = 0;
    this.countNumber = 0;
    this.orderList.forEach((v) => {
      v.opNum = v.num;
      this.countNumber += v.num;
      this.countPrice += v.total_price;
    });
  }

  ngOnInit() {

    cBack = () => {
      $('#btn-back').trigger('click');
    }

    this.extendHttp.post('/merchantApp/singleprocess/findOrderDetail', {orderId: this.orderId}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.orderList = data.orderDetail;
        this.count();
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  submitData() {
    if (!this.isLoading) {
      let paramList = [];
      this.orderList.forEach(v => {
        if (v.opNum != v.num) {
          let n = 0;
          if (v.opNum == 0) {
            n = v.num;
          } else {
            n = v.num - v.opNum;
          }
          paramList.push({detailId: v.id, num: n});
        }
      });

      this.isLoading = !this.isLoading;
      this.extendHttp.post('/merchantApp/singleprocess/deleteDetail', {
        orderId: this.orderId,
        detailJson: JSON.stringify(paramList)
      }).subscribe(response => {
        let data = response.json();
        if (data.code == '0') {
          this.submitType = false;
          this.isLoading = !this.isLoading;
          this.tipType = 'success';
          this.tipService.change(true, '订单提交成功');

          setTimeout(() => {
            this.router.navigate(['/main/table/table-list']);
          }, 1500);
        } else if (data.code == '-2001') {
          this.tipType = 'error';
          this.tipService.change(true, '登录超时，跳转到登录页面');
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 2000);
        } else {
          this.tipType = 'error';
          this.tipService.change(true, data.message);
          this.isLoading = !this.isLoading;
        }
      }, error => {
        this.tipType = 'error';
        this.tipService.change(true, '系统繁忙，操作失败！');
        this.isLoading = !this.isLoading;
      });
    }
  }
}
